package com.yanqu.road.server.cmd.activity.seacraftsouthsea;

import com.yanqu.road.entity.activity.seacraft.config.SeacraftSouthSeaActivityConfig;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.activity.SeacraftSouthSeaProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.SeacraftSouthSeaCommonMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;

/**
 * @Description
 * @Author cwq
 * @Data 2022/2/12 16:51
 */
@Cmd(code = GameProtocol.S_SEACRAFT_SOUTH_SEA_BUY_BUFF, desc = "海上争霸 南海丝路 购买buff")
public class SeacraftSouthSeaBuyBuffCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SeacraftSouthSeaProto.SeacraftSouthSeaBuyBuffReqMsg reqMsg = SeacraftSouthSeaProto.SeacraftSouthSeaBuyBuffReqMsg.parseFrom(packet.getBytes());
        ret = SeacraftSouthSeaMgr.canEntryActivity(player,reqMsg.getActivityId());
        if(ret != 0){
            return;
        }
        // 判断是否在活动期
        if(!SeacraftSouthSeaCommonMgr.isOpenTime(SeacraftSouthSeaMgr.getActivityInfo(),SeacraftSouthSeaMgr.getConfig())){
            ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_NO_OPEN;
            return;
        }

        if(!SeacraftSouthSeaMgr.isPromotion(player.getUserInfo().getUnionUid())){
            ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_TARGET_NO_PROMOTION;
            return;
        }

        SeacraftSouthSeaActivityConfig config = SeacraftSouthSeaMgr.getConfig();

        if (config.firstBuyCostList.size() < reqMsg.getBuffId() || reqMsg.getBuffId() <= 0) {
            ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_BUFF_NO_EXIST;
            return;
        }

        int preReduceCount = 0;
        long goldNum = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.Ingots);
        if(goldNum > 0){
            preReduceCount = (int)Math.min(goldNum, SeacraftSouthSeaMgr.getConfig().maxCostNum);
            Property property = new Property(GameConfig.GAME_MONEY_INGOTS, preReduceCount);
            player.getModule(PreReduceModule.class).preReduce(property);
        }


        SeacraftSouthSeaProto.SeacraftSouthSeaBuyBuffGameToCrossMsg.Builder gameToCrossMsg = SeacraftSouthSeaProto.SeacraftSouthSeaBuyBuffGameToCrossMsg.newBuilder();
        gameToCrossMsg.setActivityId(reqMsg.getActivityId());
        gameToCrossMsg.setCtsMsg(reqMsg);
        gameToCrossMsg.setPreReduceCount(preReduceCount);
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_SEACRAFT_SOUTH_SEA_BUY_BUFF, gameToCrossMsg, reqMsg.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            SeacraftSouthSeaProto.SeacraftSouthSeaBuyBuffRespMsg.Builder resp = SeacraftSouthSeaProto.SeacraftSouthSeaBuyBuffRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_SEACRAFT_SOUTH_SEA_BUY_BUFF,resp);
        }
    }
}
